package com.android.systemui.ios.notice;

import android.app.Notification;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.systemui.ios.IActionEventCallback;
import com.android.systemui.ios.IGuardService;
import com.android.systemui.ios.R;

import java.util.ArrayList;
import java.util.List;

public class AllNoticesLayout extends FrameLayout {

    private NoticeAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private final LinearLayoutManager mLayoutManager;
    private final List<NoticeData> mNoticeList = new ArrayList<>();
    private final int mItemGapHeight;

    private IGuardService mService;
    private IActionEventCallback mEventCallback;

    public AllNoticesLayout(Context context) {
        this(context, null, 0);
    }

    public AllNoticesLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AllNoticesLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mItemGapHeight = context.getResources().getDimensionPixelSize(R.dimen.notice_item_decoration_height);
        mAdapter = new NoticeAdapter(context, mNoticeList);
        mLayoutManager = new LinearLayoutManager(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mRecyclerView = findViewById(R.id.notice_list);

        mAdapter = new NoticeAdapter(getContext(), mNoticeList) {

            @Override
            public void onNoticeClicked(String key) {
                if (mEventCallback != null) {
                    mEventCallback.onOpenNotification();
                }
                if (mService != null) {
                    mService.performNotificationEvent(1, "", key);
                }
            }
        };

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0, 0, 0, mItemGapHeight);
            }
        });

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback());
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
    }

    public boolean removeNotice(NoticeData data) {
        for (int i = 0; i < mNoticeList.size(); i++) {
            if (data.isSame(mNoticeList.get(i))) {
                mNoticeList.remove(i);
                mAdapter.notifyDataSetChanged();
                return true;
            }
        }
        return false;
    }

    public void addNotice(NoticeData data) {
        if (data != null) {
            mNoticeList.add(0, data);
            if (mNoticeList.size() > 7) {
                mAdapter.notifyItemInserted(0);
            } else {
                mRecyclerView.requestLayout();
                mAdapter.notifyDataSetChanged();
            }
            mRecyclerView.scrollToPosition(0);
        }
    }

    public void updateNotice(NoticeData data) {
        removeNotice(data);
        addNotice(data);
    }

    public void setActionEventCallback(IGuardService service, IActionEventCallback callback) {
        mService = service;
        mEventCallback = callback;
    }

    public class SwipeToDeleteCallback extends ItemTouchHelper.Callback {

        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            NoticeData data = mNoticeList.get(position);
            if ((data.getFlags() & Notification.FLAG_NO_CLEAR) == 0) {
                mNoticeList.remove(position);
                mAdapter.notifyItemRemoved(position);
            }
        }

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    }
}
